/**
 * Copyright (C), 2020-2020, 软件公司
 * FileName: UserController
 * Author:   cakin
 * Date:     2020/5/5
 * Description: 用户控制器
 */
package com.cakin.newssystem.core.controller;

import com.cakin.newssystem.common.RequestException;
import com.cakin.newssystem.common.ResponseCode;
import com.cakin.newssystem.common.ResponseResult;
import com.cakin.newssystem.core.dto.SignInDTO;
import com.cakin.newssystem.core.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: UserController
 * @Description: 用户控制器
 * @Date: 2020/5/5
 * @Author: cakin
 */
@RestController
@RequestMapping(value = {"/account"})
@Slf4j
public class UserController {

    /**
     * 用户服务
     */
    @Autowired
    private UserService userService;

    @PostMapping(value = {"/sign-in"})
    public ResponseResult signIn( @RequestBody @Validated SignInDTO signInDTO ) {
        try {
            userService.signIn(signInDTO);
            return ResponseResult.e(ResponseCode.SIGN_IN_OK, "");
        } catch (RequestException e) {
            if (e.getStatus() == ResponseCode.SIGN_IN_INPUT_FAIL.getCode()) {
                return ResponseResult.e(ResponseCode.SIGN_IN_INPUT_FAIL, "");
            } else if (e.getStatus() == ResponseCode.SING_IN_INPUT_EMPTY.getCode()) {
                return ResponseResult.e(ResponseCode.SING_IN_INPUT_EMPTY, "");
            } else {
                log.info("other error");
            }
            return ResponseResult.e(ResponseCode.SIGN_IN_FAIL, "");
        }
    }

    @PostMapping(value = {"/reg"})
    public ResponseResult reg( @RequestBody SignInDTO signInDTO ) {
        try {
            userService.reg(signInDTO);
            return ResponseResult.e(ResponseCode.REG_OK, "");
        } catch (RequestException e) {
            if (e.getStatus() == ResponseCode.HAVE_REG.getCode()) {
                return ResponseResult.e(ResponseCode.HAVE_REG, "");
            } else if (e.getStatus() == ResponseCode.SING_IN_INPUT_EMPTY.getCode()) {
                return ResponseResult.e(ResponseCode.SING_IN_INPUT_EMPTY, "");
            } else {
                log.info("other error");
            }
        }
        return ResponseResult.e(ResponseCode.SIGN_IN_FAIL, "");
    }
}
